package com.rose.springbootwebsocket.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class WebSocketResponse {
    private  static Logger logger = LoggerFactory.getLogger(WebSocketResponse.class);

    private static WebSocketResponse uniqueInstance = null;

    private WebSocketResponse() {
    }

    private static WebSocketResponse getInstance() {
        if (uniqueInstance == null) {
            uniqueInstance = new WebSocketResponse();
        }
        return uniqueInstance;
    }
    private static WebSocketResponse getInstance(int code,String message,String param){
        WebSocketResponse instance = WebSocketResponse.getInstance();
        instance.setCode(code);
        instance.setMessage(message);
        instance.setParam(param);
        return instance;
    }
    public final static int SUCCESS_CODE=200; //正常
    public final static int SYS_ERROR_CODE=201; //系统异常
    public final static int DATA_ERROR_CODE=202; //数据异常
    public final static int SURPLUS_CODE=1; //剩余
    public final static int TOTAL_CODE=2; //总数
    public final static int END_TIPS_CODE=3; //提示

    public static void sendMsg(int code,String message){
        WebSocket.sendInfo(getInstance(code,message,null));
    }
    public static void sendSuccess(String message){
        WebSocket.sendInfo(getInstance(SUCCESS_CODE,message,null));
    }
    public static void sendSysError(String message,String param){
        WebSocket.sendInfo(getInstance(SYS_ERROR_CODE,message,param));
    }
    public static void sendDataError(String message){
        sendDataError(message,null);
    }
    public static void sendDataError(Exception e,String param){
        sendDataError(getExceptionInfo(e),param);
    }
    public static void sendDataError(String message,String param){
        WebSocket.sendInfo(getInstance(DATA_ERROR_CODE,message,param));
    }

    private int code;

    private String message;

    private String param;

    public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private static String getExceptionInfo(Exception ex) {
        PrintStream pout=null;
        ByteArrayOutputStream out =null;
        String ret="";
        try {
            out = new ByteArrayOutputStream();
            pout = new PrintStream(out);
            ex.printStackTrace(pout);
            ret = new String(out.toByteArray());
        } catch (Exception e) {
            logger.error("异常流转字符串出错 - 转换出错 - ExceptionUtils is error:",e);
        } finally {
            try {
                if (pout!=null){
                    pout.close();
                }
            } catch (Exception e) {
                logger.error("异常流转字符串出错 - 关闭流出错1 - ExceptionUtils is error:",e);
            }
            try {
                if (out!=null){
                    out.close();
                }
            } catch (Exception e) {
                logger.error("异常流转字符串出错 - 关闭流出错2- ExceptionUtils is error:",e);
            }
        }
        return ret;
    }
}
